<?php

class NewsList extends Controller {

	function NewsList()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('NewsModel');
	    $data['data'] = $this->NewsModel->getNews();
	    	
		$this->load->view('header');
		$this->load->view('news-list-form', $data);
		$this->load->view('footer');
	}
	
	function detail()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('NewsModel');
	    $newsID = $this->input->post('txtId'); 
	    if($newsID != null) {
			$data = $this->NewsModel->getNewsByID($newsID);		
			$this->load->view('header');
			$this->load->view('news-detail-form', $data);
			$this->load->view('footer');	    	
	    }     
	}
	
	function search()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('NewsModel');
	    $key = $this->input->post('txtTitle');
		$data['data'] = $this->NewsModel->getNewsByTitle($key);
		$data['key'] = $key;
		$this->load->view('header');
		$this->load->view('news-list-form', $data);
		$this->load->view('footer');
	}
	
	function delete(){
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('NewsModel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$this->NewsModel->deleteNews($id[$i]);
	    }	    
	    $this->search();
	}
	
}

